package sdk

import (
	"bytes"
	"fmt"
	"peilian-api/app/global/variable"

	ufsdk "github.com/ufilesdk-dev/ufile-gosdk"
)

const (
	ConfigFile   = ""
	XlsxMetaType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
)

func InitUFile(configFile string) (*ufsdk.Config, error) {
	config, err := ufsdk.LoadConfig(configFile)
	return config, err
}

// 流式上传
func IOput(data []byte, fileName string, metaType string) error {
	req, err := ufsdk.NewFileRequest(variable.UfsConfig, nil)
	if err != nil {
		fmt.Printf("IOput error :%+v", err)
		return err
	}
	err = req.IOPut(bytes.NewReader(data), fileName, metaType)
	if err != nil {
		fmt.Printf("IOput error2 :%+v", err)
		return fmt.Errorf(string(req.DumpResponse(true)))
	}
	return nil
}

// 文件上传 filePath 本地文件路径   fileName  us3文件路径
func FilePut(filePath string, fileName string, metaType string) error {
	req, err := ufsdk.NewFileRequest(variable.UfsConfig, nil)
	if err != nil {
		return err
	}
	err = req.AsyncMPut(filePath, fileName, metaType)
	return err
}
